package com.homework.module6part3.service.impl;

import com.homework.dao.domain.Goods;
import com.homework.dao.domain.Order;
import com.homework.dao.mapper.GoodsMapper;
import com.homework.dao.mapper.OrderMapper;
import com.homework.module6part3.service.OrderService;
import com.homework.util.ResultMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    protected OrderMapper orderMapper;

    @Autowired
    protected GoodsMapper goodsMapper;

    @Transactional
    public int ordering(Goods goods, int userId) {
        //扣减
        goodsMapper.deduction(goods.getId());

        //添加订单
        Order order = new Order();
        order.setUserId(userId);
        order.setGoodsId(goods.getId());
        order.setBuyNum(1);
        order.setPayAmount(goods.getPrice());
        order.setPayStatus(0);
        order.setCreateTime((int)(System.currentTimeMillis() / 1000));
        orderMapper.addOrder(order);
        return order.getId();
    }

    public Order getOrder(int orderId) {
        return orderMapper.getOrderById(orderId);
    }

    public boolean updateOrderPayStatus(int orderId, int payStatus) {
        Order order = new Order();
        order.setId(orderId);
        order.setPayStatus(payStatus);

        return orderMapper.updateOrderPayStatus(order);
    }
}
